package com.example.android.apis.view;

import com.example.android.google.apis.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class ChPopupWindow extends View {
	int screenWidth;
	int screenHeight;
	PopupWindow popupWindow;
	private int backgroundColor = Color.argb(0xaa, 0x00, 0x00, 0x00);
	private Drawable drawable;
	
	public ChPopupWindow(Context context, int screenWidth, int screenHeight) {
		super(context);
		popupWindow = new PopupWindow(context);
		popupWindow.setWidth(screenWidth);
		popupWindow.setHeight(screenHeight * 20 / 100);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.popup_window_bg));
		popupWindow.setAnimationStyle(R.style.popup_animation);
		popupWindow.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss() {
				ChPopupWindow.this.setVisibility(View.GONE);
			}
		});
		
		this.screenHeight = screenHeight;
		this.screenWidth = screenWidth;
		this.setVisibility(View.GONE);
	}

	public ChPopupWindow(Context context) {
		super(context);
	}

	public void setContentView(View contentView) {
		popupWindow.setContentView(contentView);
	}
	
	public void setBackgroundColor(int color) {
		this.backgroundColor = color;
	}
	
	public void setBackgroundDrawable(Drawable drawable) {
		this.drawable = drawable;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setARGB(0xaa, 0x00, 0x00, 0x00);
		canvas.drawRect(new RectF(0, 0, screenWidth, screenHeight), paint);
		canvas.save();
		
		super.onDraw(canvas);
	}
	
	public void show(View parent, int x, int y) {
		this.setVisibility(View.VISIBLE);
		popupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, x, y);
	}
}
